<?php
require_once('includes/bootstrap.php');

if (empty($_SESSION[SESSION_NAME]))
{
	header("HTTP/1.0 404 Not Found");
	exit;
}

?>

<html>
	<head>
		<?php include_once('includes/head.php'); ?>
	</head>

	<body>

		<?php include_once('includes/menu_admin.php'); ?>

		<h1>Clients</h1>

		<table class="display dataTable">
			<thead>
				<tr>
					<th>Email</th>
					<th>Nom</th>
					<th>Prénom</th>
					<th>Adresse</th>
					<th>Téléphone</th>
					<th>Nb commandes</th>
					<th>Coût total</th>
				</tr>
			</thead>

			<tbody>
				<?php
				$req = mysql_query('SELECT clients.*, COUNT(orders.id) "count", SUM(price) "price"
														FROM clients 
														LEFT JOIN orders ON orders.client_id = clients.id 
														GROUP BY clients.id 
														ORDER BY name ASC');

				$i=0;

				while ($row = mysql_fetch_assoc($req))
				{
					if ($i%2 == 0)
						echo '<tr class="gradeA even">';
					else
						echo '<tr class="gradeA odd">';

					echo '	<td>' . $row['email'] . '</td>';
					echo '	<td>' . $row['name'] . '</td>';
					echo '	<td>' . $row['firstname'] . '</td>';
					echo '	<td>' . nl2br($row['address']) . '</td>';
					echo '	<td>' . $row['phone_number'] . '</td>';
					echo '	<td><a href="' . APPLICATION_URL . 'orders.php?client_id=' . $row['id'] . '">' . $row['count'] . '</a></td>';
					if ($row['price'] > 0)
						echo '	<td><a href="' . APPLICATION_URL . 'orders.php?client_id=' . $row['id'] . '">' . $row['price'] . '€</a></td>';
					else
						echo '	<td> </td>';
					echo '</tr>';

					$i++;
				}

				?>
			</tbody>

		</table>

		<script type="text/javascript">
			$('table').dataTable({
				"iDisplayLength": 20,
				"bJQueryUI": true
			});
		</script>

	</body>

</html>
